package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogMyBinding;

/**
 * 通用弹窗
 */
public class MyDialog {
    private Dialog dialog;
    private DialogMyBinding binding;
    private boolean isCancel = true;
    private int count = 2;

    public MyDialog(Activity activity) {
        if (dialog == null) {
            getDialog(activity);
        }
    }

    public MyDialog(Activity activity, boolean isCancel) {
        this.isCancel = isCancel;
        if (dialog == null) {
            getDialog(activity);
        }
    }

    public MyDialog(Activity activity, int count) {
        this.count = count;
        if (dialog == null) {
            getDialog(activity);
        }
    }

    private void getDialog(Activity activity) {
        binding = DialogMyBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(isCancel);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        // 显示一个确定按钮
        if (count == 1) {
            binding.tvLeft.setVisibility(View.GONE);
        }
        binding.tvLeft.setOnClickListener(v -> {
            leftClick();
            dismissDialog();
        });
        binding.tvRight.setOnClickListener(v -> {
            rightClick();
            dismissDialog();
        });
    }

    public void leftClick(){

    }

    public void rightClick(){

    }

    public void setText(String title, String content, String leftText, String rightText) {
        binding.tvTitle.setText(title);
        binding.tvContent.setText(content);
        if (TextUtils.isEmpty(leftText)) {
            binding.tvLeft.setVisibility(View.GONE);
        } else {
            binding.tvLeft.setText(leftText);
        }
        binding.tvRight.setText(rightText);
    }

    public void setContent(String content) {
        binding.tvContent.setText(content);
    }

    public void setText(String title, String content) {
        binding.tvTitle.setText(title);
        binding.tvContent.setText(content);
    }

    public void setIconVisibility(int visibility) {
        binding.ivIcon.setVisibility(visibility);
    }

    public void setCancelHide() {
        binding.tvLeft.setVisibility(View.GONE);
    }

    public void setCancel(boolean isCancel) {
        dialog.setCancelable(isCancel);
    }

    public void setCancelShow() {
        binding.tvLeft.setVisibility(View.VISIBLE);
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
